package cn.dapeng.features;

import java.util.concurrent.CountDownLatch;

/**
 * 线程是顺势执行还是乱序执行？？
 */
public class ThreadOrder {

    private static int x = 0, y = 0;
    private static int a = 0, b = 0;

    public static void main(String[] args) throws InterruptedException {
        for (long i = 0; i < Long.MAX_VALUE; i++) {
            x = 0;
            y = 0;
            a = 0;
            b = 0;
            CountDownLatch countDownLatch = new CountDownLatch(2);
            new Thread(() -> {
                a = 1;
                x = b;
                countDownLatch.countDown();
            }).start();

            new Thread(() -> {
                b = 1;
                y = a;
                countDownLatch.countDown();
            }).start();

            countDownLatch.await();

            String result = "循环" + i + "次" + "\t x = " + x + "\t y = " + y;
            if (x == 0 && y == 0) {
                System.out.println(result);
                break;
            }
        }
    }
}
